﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class RequestActivity : System.Web.UI.Page
    {
        //IEmployeeBLL EmpBLL = new EmployeeBLL();
        B_HrmPositions posBLL = new B_HrmPositions();
        B_MessageSend sendBLL = new B_MessageSend();
        HrmActivityBLL actBLL = new HrmActivityBLL();
        ActivityRequestBLL ActReqBLL = new ActivityRequestBLL();

        private const int ItemsPerRequest = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        private void LoadDataGrid()
        {
            DataSet ListDataCommand = ActReqBLL.GetActRequest(null, Int32.Parse(Session["EmpSeq"].ToString()), null);
            RadGrid2.DataSource = ListDataCommand;
            RadGrid2.DataBind();
        }
        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {

            }
            if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
            }
            LoadDataGrid();
        }
        protected void cmbActivity_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDetail();
        }
        protected void LoadDetail()
        {
            int Sequence = Int32.Parse(cmbActivity.SelectedValue);
            DataSet ListDataCommand = actBLL.LoadActivity(Sequence, (int?)null, (DateTime?)null, (DateTime?)null);
            if (ListDataCommand.Tables.Count > 0)
            {
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                {
                    lbActDescr.Text = table.Rows[0]["act_descr"].ToString();
                    lblJobsite.Text = table.Rows[0]["job_site_name"].ToString();
                    lbDateSignStard.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_start"]);
                    lbDateSignEnd.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_end"]);
                    lbDateActStard.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_activity_start"]);
                    lbDateActEnd.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_activity_end"]);
                    lbActLimit.Text = table.Rows[0]["act_limit"].ToString();
                    //lbAccepTrainee.Text = table.Rows[0]["Total"].ToString();
                    lblLocation.Text = table.Rows[0]["location_text"].ToString();
                    lbTotalTrainee.Text = table.Rows[0]["Candidate"].ToString();
                    lblCity.Text = table.Rows[0]["city_name"].ToString();
                    lblCountry.Text = table.Rows[0]["country_name"].ToString();

                }
            }
        }
        protected void cmbActivity_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbActivity.Items.Clear();

            DataSet ds = actBLL.LoadActivity(null, null, null, null);            
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                int itemOffset = e.NumberOfItems;
                int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
                e.EndOfItems = (endOffset == dt.Rows.Count);
                int j = 0;
                for (int i = itemOffset; i < endOffset; i++)
                {
                    cmbActivity.Items.Add(new RadComboBoxItem(dt.Rows[i]["BGM_PROJ_OPER_NAME"].ToString(), dt.Rows[i]["ACT_SEQ"].ToString()));
                    j++;
                }
            }
        }
        protected void cmbAppSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbAppSeq.Items.Clear();

            DataSet ds = null;
            ds = posBLL.GetPositionUpper(Int32.Parse(Session["EmpSeq"].ToString()), (int?)null, e.Text);
            DataTable dt = null;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

                int itemOffset = e.NumberOfItems;
                int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
                e.EndOfItems = (endOffset == dt.Rows.Count);
                int j = 0;
                for (int i = itemOffset; i < endOffset; i++)
                {
                    cmbAppSeq.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["UPPER_EMP_SEQ"].ToString()));
                    j++;
                }
            }
        }
        protected void SendMessage()
        {
            M_MessageSend info = new M_MessageSend();

            info.from_emp_seq = Int32.Parse(Session["EmpSeq"].ToString());
            info.from_pos_seq = sendBLL.GetPosSeqFromEmpSeq(Session["EmpSeq"].ToString());
            info.to_emp_seq = Int32.Parse(cmbAppSeq.SelectedValue);
            info.to_pos_seq = sendBLL.GetPosSeqFromEmpSeq(cmbAppSeq.SelectedValue);
            info.ref_seq = ActReqBLL.GetLastInsertActivityRequest();
            info.ref_type = "HRM_AC_ACTIVITYS";
            info.send_date = DateTime.Today;
            info.send_text = "ขออนุมัติเข้าร่วม" + cmbActivity.Text;
            info.message_status = "0";

            sendBLL.Insert(info);
        }
        protected void btnSave2_Click(object sender, EventArgs e)
        {
            ActReqBLL.InsertActRequest(cmbActivity.SelectedValue
                , Session["EmpSeq"].ToString()
                , "0"
                , ""
                , Session["EmpSeq"].ToString());
            SendMessage();
            LoadDataGrid();
            UpdateUI2("Save");
        }
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
        }
        virtual
            protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData2.Visible = true;
                    btnSave2.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData2.Visible = true;
                    btnSave2.Visible = false;

                    break;
            }
        }
    }
}
